package com.camus.verify.service.impl;

import java.time.LocalDateTime;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.camus.verify.entity.VerifyTask;
import com.camus.verify.enums.VerifyTaskStatusEnum;
import com.camus.verify.mapper.VerifyTaskMapper;
import com.camus.verify.service.IVerifyTaskService;

/**
 * <p>
 * 核销任务 服务实现类
 * </p>
 *
 * @author jie.deng
 * @since 2021-09-04
 */
@Service
public class VerifyTaskServiceImpl extends ServiceImpl<VerifyTaskMapper, VerifyTask> implements IVerifyTaskService {

	@Override
	public VerifyTask getByTaskCode(String taskCode) {
		LambdaQueryWrapper<VerifyTask> queryWrapper = Wrappers.lambdaQuery(VerifyTask.class).eq(VerifyTask::getTaskCode,
				taskCode);
		VerifyTask verifyTask = getBaseMapper().selectOne(queryWrapper);
		return verifyTask;
	}

	@Override
	public int startTask(VerifyTask task) {
		LambdaUpdateWrapper<VerifyTask> updateWrapper = Wrappers.lambdaUpdate(VerifyTask.class);
		updateWrapper.ne(VerifyTask::getTaskStatus, VerifyTaskStatusEnum.Ing.getCode());
		updateWrapper.eq(VerifyTask::getPkid, task.getPkid());

		VerifyTask updateTask = new VerifyTask();
		updateTask.setExecStartTime(LocalDateTime.now());
		updateTask.setTaskStatus(VerifyTaskStatusEnum.Ing.getCode());
		// updateTask.setLastModifiedTime(LocalDateTime.now());

		return getBaseMapper().update(updateTask, updateWrapper);
	}

	@Override
	public int endTask(VerifyTask task) {
		LambdaUpdateWrapper<VerifyTask> updateWrapper = Wrappers.lambdaUpdate(VerifyTask.class);
		updateWrapper.eq(VerifyTask::getTaskStatus, VerifyTaskStatusEnum.Ing.getCode());
		updateWrapper.eq(VerifyTask::getPkid, task.getPkid());

		VerifyTask updateTask = new VerifyTask();
		updateTask.setExecEndTime(LocalDateTime.now());
		updateTask.setTaskStatus(task.getTaskStatus());
		// updateTask.setLastModifiedTime(LocalDateTime.now());

		return getBaseMapper().update(updateTask, updateWrapper);
	}

}
